#include "Irp.h"

NTSTATUS IrpGeneral(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);
    UNREFERENCED_PARAMETER(Irp);
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}

NTSTATUS IrpDeviceControl(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION pStackLocation = IoGetCurrentIrpStackLocation(Irp);
    ULONG uCode = pStackLocation->Parameters.DeviceIoControl.IoControlCode;
    ULONG uLen = pStackLocation->Parameters.DeviceIoControl.InputBufferLength;
    PVOID pBuff = Irp->AssociatedIrp.SystemBuffer;

    switch (uCode)
    {
    case CTL_CODE_PRINT:
    {
        DBGPRINT("%s\r\n", pBuff);
        RtlZeroMemory(pBuff, uLen);
        RtlCopyMemory(pBuff, "Success", strlen("Success"));

        Irp->IoStatus.Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = strlen("Success");
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
    }
    default:
        ASSERTMSG(FALSE, __FUNCTION__);
        break;
    }

    return ntStatus;
}

NTSTATUS IrpWrite(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION pStackLocation = IoGetCurrentIrpStackLocation(Irp);
    PVOID pBuff = Irp->AssociatedIrp.SystemBuffer;

    DBGPRINT("%s, %d\r\n", pBuff, pStackLocation->Parameters.Write.Length);

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = pStackLocation->Parameters.Write.Length;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return ntStatus;
}

NTSTATUS IrpRead(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PVOID pBuff = Irp->AssociatedIrp.SystemBuffer;

    RtlCopyMemory(pBuff, "Microsoft", strlen("Microsoft"));

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = strlen("Microsoft");
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return ntStatus;
}


NTSTATUS IrpDeviceControl2(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION pStackLocation = IoGetCurrentIrpStackLocation(Irp);
    ULONG uCode = pStackLocation->Parameters.DeviceIoControl.IoControlCode;
    PVOID pBuff = Irp->AssociatedIrp.SystemBuffer;
    PVOID pOutBuff = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

    switch (uCode)
    {
    case CTL_CODE_PRINT2:
    {
        DBGPRINT("%s\r\n", pBuff);
        RtlCopyMemory(pOutBuff, "Success", strlen("Success"));

        Irp->IoStatus.Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = strlen("Success");
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
    }
    default:
        ASSERTMSG(FALSE, __FUNCTION__);
        break;
    }

    return ntStatus;
}

NTSTATUS IrpWrite2(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PIO_STACK_LOCATION pStackLocation = IoGetCurrentIrpStackLocation(Irp);
    PVOID pOutBuff = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

    DBGPRINT("%s, %d\r\n", pOutBuff, pStackLocation->Parameters.Write.Length);

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = pStackLocation->Parameters.Write.Length;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return ntStatus;
}

NTSTATUS IrpRead2(IN struct _DEVICE_OBJECT* DeviceObject, IN OUT struct _IRP* Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    NTSTATUS ntStatus = STATUS_SUCCESS;
    PVOID pOutBuff = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);

    RtlCopyMemory(pOutBuff, "Microsoft", strlen("Microsoft"));

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = strlen("Microsoft");
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return ntStatus;
}
